package net.ak.controller;

import java.util.Date;
import java.util.List;

import net.ak.constant.Constant;
import net.ak.model.Category;
import net.ak.response.AKResponse;
import net.ak.service.CategoryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api/category")
public class CategoryController extends BaseController {
	
	@Autowired
	private CategoryService categoryService;

	/**
	 * This method is used to get all {@link Category}
	 * @return
	 */
	@RequestMapping(value = "/listall", method = RequestMethod.GET)
	public @ResponseBody AKResponse listAllCategories() {
		AKResponse akResponseSuccess = new AKResponse(Constant.SUCCESS_CODE, Constant.SUCCESS_MESSAGE);
		akResponseSuccess.setResponseData(categoryService.listCategories());
		return akResponseSuccess;
	}
	
	
	/**
	 * This method is used to add {@link Category} into the database.
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody AKResponse addCategory(@RequestParam String name, @RequestParam String description) {
		AKResponse akResponseSuccess 	= new AKResponse(Constant.SUCCESS_CODE, Constant.SUCCESS_MESSAGE);
		Category category 				= new Category();
		
		category.setCreatedDate(new Date());
		category.setName(name);
		category.setDescription(description);
		
		boolean isInsertSuccess = categoryService.saveOrUpdate(category);
		
		if (isInsertSuccess) {
			return akResponseSuccess;
		} else {
			return new AKResponse(Constant.INSERT_ERROR_CODE, Constant.INSERT_ERROR_MESSAGE);
		}
	}
	
	/**
	 * This method is used to search {@link Category} by name.
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public @ResponseBody AKResponse searchCategory(@RequestParam String name) {
		AKResponse akResponseSuccess 	= new AKResponse(Constant.SUCCESS_CODE, Constant.SUCCESS_MESSAGE);
		List<Category> categories 		= categoryService.searchCategoryByName(name);
		akResponseSuccess.setResponseData(categories);
		return akResponseSuccess;
	}
	
	/**
	 * This method is used to check exist {@link Category}
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/checkexist", method = RequestMethod.GET)
	public @ResponseBody AKResponse checkExistCategory(@RequestParam String name) {
		Category category 				= categoryService.getCategoryByName(name);
		if (category == null) {
			AKResponse akResponseSuccess 	= new AKResponse(Constant.EXIST_CATEGORY_CODE, Constant.EXIST_CATEGORY_MESSAGE);
			return akResponseSuccess;
		}
		
		AKResponse akResponseSuccess 	= new AKResponse(Constant.SUCCESS_CODE, Constant.SUCCESS_MESSAGE);
		return akResponseSuccess;
	}
}
